//
//  NYTopicManager.m
//  Nooryes
//
//  Created by baken on 2020/3/4.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYTopicManager.h"
#import "NYTopicListModel.h"
#import "NYArticleListModel.h"
#import "NYTopicDetailModel.h"

#import "NYAccountModel.h"

@implementation NYTopicManager


+ (void)getTopicListWithPage:(NSString *)page andCompletionHandle:(void (^)(NYTopicListModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?p=%@",Api_URL,kGetTopicList,page]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYTopicListModel *model = [NYTopicListModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}


+ (void)getTopicArticleWithTopicID:(NSString *)ID andPage:(NSString *)page andOrder:(NSString *)order andCompletionHandle:(void (^)(NYArticleListModel *model, NSError *error))complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:page forKey:@"p"];
    [dict setValue:order forKey:@"order"];
    [dict setValue:ID forKey:@"talkid"];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [self POST:[NSString stringWithFormat:@"%@%@?",Api_URL,kGetTopiArticlecList]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYArticleListModel *model = [NYArticleListModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}

+ (void)getTopicDetailWithTopicID:(NSString *)ID andCompletionHandle:(void (^)(NYTopicDetailModel *model, NSError *error))complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:ID forKey:@"talkid"];
    [dict setValue:uid forKey:@"userid"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kGetTopicDetail]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYTopicDetailModel *model = [NYTopicDetailModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}

+(void)getOtherUserInfoWithUserID:(NSString *)ID andCompletionHandle:(void (^)(NYAccountModel * _Nonnull, NSError * _Nonnull))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?userid=%@",Api_URL,kGetOtherUserInfo,ID]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYAccountModel *model = [NYAccountModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}

+(void)getOtherUserArticlesWithUserID:(NSString *)ID andPage:(NSString *)page andCompletionHandle:(void (^)(NYArticleListModel * _Nonnull, NSError * _Nonnull))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?userid=%@&p=%@",Api_URL,kGetOtherUserArticles,ID,page]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
        NYArticleListModel *model = [NYArticleListModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
}


@end
